home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 April: Mac OS SDK / Dev.CD Apr 98 SDK2.toast / Development Kits (Disc 2) / QuickTake Digital Camera / MyQuickTakeApp 1.0.1TC7 ƒ / Sources / CameraUtils.c next >
Encoding:
C/C++ Source or Header  |  1995-03-10  |  31.8 KB  |  1,210 lines  |  [TEXT/MMCC]

  1. /*
  2.     File:        Camera.c
  3.     
  4.     Contains:    My Application Shell.
  5.  
  6.     Written by:    John Wang
  7.  
  8.     Copyright:    © 1994 by Apple Computer, Inc., all rights reserved.
  9.  
  10.     Change History (most recent first):
  11.  
  12.         <1>        09/30/94    JW        New.
  13.  
  14.     To Do:
  15.     
  16. */
  17.  
  18. #ifdef THINK_C
  19. #define        applec
  20. #endif
  21.  
  22. #include    "CMDriver.h"
  23. #include    "GeoPortErrors.h"
  24.  
  25. #include    "QuickTakePICT.h"
  26. #include    "CameraUtils.h"
  27.  
  28. /* ------------------------------------------------------------------------- */
  29. /* ------------------------------------------------------------------------- */
  30. /* ------------------------------------------------------------------------- */
  31.  
  32. //    These routines are for opening the driver, connecting, disconnecting, and closing the driver.
  33.  
  34. /*
  35.     Description:    OpenCameraDriver()
  36.  
  37.     Format Params:    
  38.         Name            Usage    Description/Assumptions
  39.         ----            ----    -----------------------
  40.         camData            PO        Returned.
  41.                                 
  42.     Usage: P=Parameter,R=ReturnValue,E=External,G=FileGlobal,L=Local,I=Input,O=Output
  43.     
  44.     Error Handling:    If noErr is returned, you can assume that the camera driver was opened properly.
  45.                     If err is returned, *camData will be nil.
  46.  
  47.     Special Notes:    If this fails, you don't need to call CloseCameraDriver.
  48.  
  49. */
  50.  
  51. OSErr OpenCameraDriver(CameraData *camData)
  52. {
  53.     OSErr                err;
  54.     
  55.     //    Check input.
  56.     if ( camData == nil )
  57.         return ( paramErr );
  58.     
  59.     //    Initialize.
  60.     *camData = nil;
  61.     
  62.     //    Open driver.
  63.     err = CmOpenDriver(camData);
  64.     if ( err != noErr ) {
  65.         *camData = nil;
  66.         goto bail;
  67.     }
  68.     if ( *camData == nil ) {
  69.         err = -1;
  70.         goto bail;
  71.     }
  72.     
  73.     //    Done!
  74.     return ( noErr );
  75.     
  76. bail:    
  77.     //    Error!
  78.     return ( err );
  79. }
  80.  
  81. /* ------------------------------------------------------------------------- */
  82.  
  83. /*
  84.     Description:    CloseCameraDriver()
  85.  
  86.     Format Params:    
  87.         Name            Usage    Description/Assumptions
  88.         ----            ----    -----------------------
  89.         camData            PI        Valid camData from OpenCameraDriver.
  90.                                 
  91.     Usage: P=Parameter,R=ReturnValue,E=External,G=FileGlobal,L=Local,I=Input,O=Output
  92.     
  93.     Error Handling:    If noErr is returned, you can assume that the camera driver was closed properly.
  94.  
  95.     Special Notes:    Returns error.
  96.  
  97. */
  98.  
  99. OSErr CloseCameraDriver(CameraData camData)
  100. {
  101.     OSErr                err;
  102.  
  103.     //    Close driver.
  104.     if ( camData != nil )
  105.         err = CmCloseDriver(camData);
  106.     else
  107.         err = paramErr;
  108.         
  109.     return ( err );
  110. }
  111.  
  112. /* ------------------------------------------------------------------------- */
  113.  
  114. /*
  115.     Description:    ConnectCamera()
  116.  
  117.     Format Params:    
  118.         Name            Usage    Description/Assumptions
  119.         ----            ----    -----------------------
  120.         camData            PI        Valid camData from OpenCameraDriver.
  121.         portInfoHandle    PO        Returned.
  122.         cameraType        PO        Returned if not nil.  See include file for camera types.
  123.                                 
  124.     Usage: P=Parameter,R=ReturnValue,E=External,G=FileGlobal,L=Local,I=Input,O=Output
  125.     
  126.     Error Handling:    If noErr is returned, you can assume that the camera was connected properly.
  127.                     If error occured, *portInfoHandle will be nil.
  128.  
  129.     Special Notes:    Does not close camera driver.
  130.  
  131. */
  132.  
  133. OSErr ConnectCamera(CameraData camData, CmPortInfoHandle *portInfoHandle, short *cameraType)
  134. {
  135.     OSErr                err;
  136.     unsigned long        portIndex;
  137.     Boolean                foundCamera;
  138.     short                tempCameraType;
  139.     
  140.     //    Initialize.
  141.     if ( portInfoHandle == nil || camData == nil ) {
  142.         err = paramErr;
  143.         goto bail;
  144.     }
  145.     
  146.     //    Keep looking until either an error occurs, or a camera is found.
  147.     portIndex = 1;
  148.     foundCamera = false;
  149.     while ( (!foundCamera) ) {
  150.         //    Get info.
  151.         err = CmGetPortInfo(camData, portIndex, portInfoHandle);
  152.         
  153.         //    If an error occured, then assume there are no more ports to check.  In this case, we exit.
  154.         if ( err != noErr ) {
  155.             //    No more ports to check.
  156.             *portInfoHandle = nil;
  157.             goto bail;
  158.         }
  159.         
  160.         //    Double check to make sure the info handle is not nil.
  161.         if ( *portInfoHandle == nil ) {
  162.             err = paramErr;
  163.             goto bail;
  164.         }
  165.         portIndex++;
  166.         
  167.         //    If we've gotten this far, then it means we have information for one port.  Now, let's see if there is
  168.         //    QuickTake camera connected to a free port.  If not, then camera is NOT found.
  169.         if ((**(*portInfoHandle)).portStatus == kGeoPortFree ) {
  170.             //    Check for camera and Return cameraType if requested.
  171.             
  172.             //    Code for QuickTake™ 150 driver.
  173.             #if 1
  174.                 if ( (**(*portInfoHandle)).manufacturerID == 0x101 && (**(*portInfoHandle)).productID == 0x100 ) {
  175.                     tempCameraType = kVenusCamera;
  176.                     foundCamera = true;
  177.                 } else if ( (**(*portInfoHandle)).manufacturerID == 0x101 && (**(*portInfoHandle)).productID == 0x101 ) {
  178.                     tempCameraType = kVenusCamera;
  179.                     foundCamera = true;
  180.                 } else if ( (**(*portInfoHandle)).manufacturerID == 0xc8 && (**(*portInfoHandle)).productID == 0x1 ) {
  181.                     tempCameraType = kNimbusCamera;
  182.                     foundCamera = true;
  183.                 } else {
  184.                     tempCameraType = kUnknownCamera;
  185.                 }
  186.             //    Code for OLD QuickTake™ 100 driver.
  187.             #else
  188.                 if ( (**(*portInfoHandle)).beaconParam == 0x100 || (**(*portInfoHandle)).beaconParam == 0x101 ) {
  189.                     tempCameraType = kVenusCamera;
  190.                     foundCamera = true;
  191.                 } else if ( (**(*portInfoHandle)).beaconParam == 0x200 ) {
  192.                     tempCameraType = kNimbusCamera;
  193.                     foundCamera = true;
  194.                 } else {
  195.                     tempCameraType = kUnknownCamera;
  196.                 }
  197.             #endif
  198.         }
  199.         
  200.         //    Dispose of the info handle if camera is NOT found.  We return the info handle if there camera is found.5
  201.         if ( foundCamera == false ) {
  202.             err = CmDisposePortInfo(camData, *portInfoHandle);
  203.             *portInfoHandle = nil;
  204.             if ( err != noErr )
  205.                 goto bail;
  206.         }
  207.     }
  208.     
  209.     //    Connect.
  210.     err = CmConnect(camData, *portInfoHandle);
  211.     if ( err != noErr )
  212.         goto bail;
  213.  
  214.     if ( cameraType != nil ) {
  215.         *cameraType = tempCameraType;
  216.     }
  217.  
  218.     return ( noErr );
  219.     
  220. bail:
  221.     DisconnectCamera(camData, *portInfoHandle);
  222.  
  223.     *portInfoHandle = nil;
  224.     
  225.     return ( err );
  226. }
  227.  
  228. /* ------------------------------------------------------------------------- */
  229.  
  230. /*
  231.     Description:    DisconnectCamera()
  232.  
  233.     Format Params:    
  234.         Name            Usage    Description/Assumptions
  235.         ----            ----    -----------------------
  236.         camData            PI        Valid camData from OpenCameraDriver.
  237.         portInfoHandle    PI        Valid portInfoHandle from ConnectCamera.
  238.                                 
  239.     Usage: P=Parameter,R=ReturnValue,E=External,G=FileGlobal,L=Local,I=Input,O=Output
  240.     
  241.     Error Handling:    Completes disconnect even if error is returned.
  242.  
  243.     Special Notes:    xxx put other comments here xxx
  244.  
  245. */
  246.  
  247. OSErr DisconnectCamera(CameraData camData, CmPortInfoHandle    portInfoHandle)
  248. {
  249.     OSErr                err1, err2;
  250.     
  251.     if ( camData != nil && portInfoHandle != nil) {
  252.         err1 = CmDisposePortInfo(camData, portInfoHandle);
  253.     }
  254.     if ( camData != nil ) {
  255.         err2 = CmDisconnect(camData);
  256.     }
  257.     
  258.     if ( err1 != noErr )
  259.         return ( err1 );    
  260.     return ( err2 );    
  261. }
  262.  
  263. /* ------------------------------------------------------------------------- */
  264. /* ------------------------------------------------------------------------- */
  265. /* ------------------------------------------------------------------------- */
  266.  
  267. //    These routines require you to connect and disconnect to the driver youself.
  268.  
  269. /*
  270.     Description:    GrabOnePicture()
  271.                     Given and image number, load a pict, thumbnail,
  272.                     thumbnailHeader, and pictureInfo as requested.
  273.  
  274.     Format Params:    
  275.         Name            Usage    Description/Assumptions
  276.         ----            ----    -----------------------
  277.         camData            PI        Valid camData from OpenCameraDriver.
  278.         imageNumber        PI        Pass in an image number.
  279.         pict            PO        If pict is not nil, then it will be returned.  The pict is unlocked.
  280.         thumbnail        PO        thumbnail is not nil, then it will be returned.  The thumbnail is unlocked.
  281.         thumbnailHeader    PO        If thumbnailHeader is not nil then it will be returned.
  282.         pictureInfo        PO        if pictureInfo is not nil, then it will be returned.
  283.                                 
  284.     Usage: P=Parameter,R=ReturnValue,E=External,G=FileGlobal,L=Local,I=Input,O=Output
  285.     
  286.     Error Handling:    If noErr is returned, you can assume that everything requested has been returned.
  287.  
  288.     Special Notes:    xxx put other comments here xxx
  289.  
  290. */
  291.  
  292. OSErr GrabOnePicture(CameraData camData, short cameraType, short imageNumber, PicHandle *pict, Handle *thumbnail,
  293.                         ThumbnailHeader *thumbnailHeader, CmPictureInfo *pictureInfo)
  294. {
  295.     OSErr                    err;
  296.     Handle                    imageBuffer;
  297.     Handle                    *imageBufferPtr;
  298.     Handle                    thumbnailBuffer;
  299.     Handle                    *thumbnailBufferPtr;
  300.     ImageHeader                imageHeader;
  301.     CmPictureInfo            tempPictureInfo;
  302.     CmColorMatrix            colorMatrix;
  303.     BufferPtr                decompTable;
  304.     Boolean                    customColorMatrix;
  305.     
  306.     //    Make some temporary variables.
  307.     decompTable = nil;
  308.     imageBuffer = nil;
  309.     if ( pict != nil ) {
  310.         imageBufferPtr = &imageBuffer;
  311.     } else
  312.         imageBufferPtr = nil;
  313.     thumbnailBuffer = nil;
  314.     if ( thumbnail != nil ) {
  315.         thumbnailBufferPtr = &thumbnailBuffer;
  316.     } else
  317.         thumbnailBufferPtr = nil;
  318.         
  319.     //    Get image or thumbnail as requested.
  320.     err = GrabOneImage(camData, imageNumber, imageBufferPtr, thumbnailBufferPtr, &tempPictureInfo);
  321.     if ( err != noErr ) {
  322.         goto bail;
  323.     }
  324.     
  325.     //    Make sure data is returned properly.
  326.     if ( pict != nil ) {
  327.         if (imageBuffer == nil ) {
  328.             err = memFullErr;
  329.             goto bail;
  330.         }
  331.     }
  332.     if ( thumbnail != nil ) {
  333.         if (thumbnailBuffer == nil ) {
  334.             err = memFullErr;
  335.             goto bail;
  336.         }
  337.     }
  338.     
  339.     //    Build imageHeader no matter what.
  340.     if ( cameraType == kVenusCamera ) {
  341.         imageHeader.signature = 'qktk';
  342.         imageHeader.version = 1;
  343.     } else if ( cameraType == kNimbusCamera ) {
  344.         imageHeader.signature = 'qktn';
  345.         imageHeader.version = 2;
  346.     } else {
  347.         err = paramErr;
  348.         goto bail;
  349.     }
  350.     if ( tempPictureInfo.pictureMode == 16 )
  351.         imageHeader.flags = kQktkHiResImage;
  352.     else if ( tempPictureInfo.pictureMode == 32 )
  353.         imageHeader.flags = kQktkStdResImage;
  354.     else {
  355.         err = paramErr;
  356.         goto bail;
  357.     }
  358.     imageHeader.dataSize = tempPictureInfo.imageDataSize;
  359.         
  360.     customColorMatrix = false;
  361.     if ( pict != nil ) {
  362.         //    Get matrix if it is newer and if a pict is requested.
  363.         err = GetCameraMatrix(camData, &colorMatrix);
  364.         if ( err == noErr )
  365.             customColorMatrix = true;
  366.         else if ( err != -1 )
  367.             goto bail;
  368.  
  369.         //    Get decompTable if it is newer and if a pict is requested.
  370.         if ( cameraType == kVenusCamera ) {
  371.             decompTable = (BufferPtr) NewPtr(kCmDecompTableSize100);
  372.         } else if  ( cameraType == kNimbusCamera ) {
  373.             decompTable = (BufferPtr) NewPtr(kCmDecompTableSize150);
  374.         } else {
  375.             err = paramErr;
  376.             goto bail;
  377.         }
  378.         if ( decompTable == nil ) {
  379.             err = memFullErr;
  380.             goto bail;
  381.         }
  382.         err = GetCameraDecompTable(camData, decompTable);
  383.         if ( err == -1 ) {
  384.             if ( decompTable != nil )
  385.                 DisposePtr((Ptr) decompTable);
  386.             decompTable = nil;
  387.         } else if ( err != noErr )
  388.             goto bail;
  389.     }
  390.     
  391.     //    Make the pict using imageHeader.
  392.     if ( pict != nil ) {
  393.         CmColorMatrixPtr            colorMatrixPtr;
  394.         
  395.         if ( customColorMatrix )
  396.             colorMatrixPtr = &colorMatrix;
  397.         else
  398.             colorMatrixPtr = nil;
  399.         HLock(imageBuffer);
  400.         err = MakeQuickTakePicture(&imageHeader, *imageBuffer, colorMatrixPtr, decompTable, pict);
  401.         HUnlock(imageBuffer);
  402.         if ( err != noErr ) {
  403.             goto bail;
  404.         }
  405.     }
  406.     
  407.     //    Make thumbnail.
  408.     if ( thumbnail != nil ) {
  409.         HLock(thumbnailBuffer);
  410.         *thumbnail = thumbnailBuffer;
  411.         HUnlock(thumbnailBuffer);
  412.     }
  413.  
  414.     //    Make thumbnail header.
  415.     if ( thumbnailHeader != nil ) {
  416.         thumbnailHeader->signature = imageHeader.signature;
  417.         thumbnailHeader->version = imageHeader.version;
  418.         thumbnailHeader->flags = kQktkThumbnailImage | (imageHeader.flags & kQktkRotationBits);
  419.         thumbnailHeader->dataSize = 2400;
  420.     }
  421.     
  422.     //    Fill in pictureInfo.
  423.     if ( pictureInfo != nil ) {
  424.         *pictureInfo = tempPictureInfo;
  425.     }
  426.     
  427.     err = noErr;
  428.     
  429. bail:
  430.     if ( decompTable != nil )
  431.         DisposePtr((Ptr) decompTable);
  432.  
  433.     //    imageBuffer is no longer needed because it is stored into Picture.
  434.     if ( imageBuffer != nil )
  435.         DisposHandle(imageBuffer);
  436.         
  437.     return ( err );
  438. }
  439.  
  440. /* ------------------------------------------------------------------------- */
  441.  
  442. /*
  443.     Description:    GrabOneImage()
  444.                     Given and image number, load imageBuffer, thumbnailBuffer, and pictureInfo
  445.                     as requested.
  446.  
  447.     Format Params:    
  448.         Name            Usage    Description/Assumptions
  449.         ----            ----    -----------------------
  450.         camData            PI        Valid camData from OpenCameraDriver.
  451.         imageNumber        PI        Pass in an image number.
  452.         imageBuffer        PO        If imageBuffer is not nil, then it will be returned.
  453.         thumbnailBuffer    PO        If thumbnailBuffer is not nil, then it will be returned.
  454.         pictureInfo        PO        pictureInfo is not nil, then then it will be returned.
  455.                                 
  456.     Usage: P=Parameter,R=ReturnValue,E=External,G=FileGlobal,L=Local,I=Input,O=Output
  457.     
  458.     Error Handling:    If noErr is returned, you can assume that everything requested has been returned.
  459.  
  460.     Special Notes:    xxx put other comments here xxx
  461.  
  462. */
  463.  
  464. OSErr GrabOneImage(CameraData camData, short imageNumber, Handle *imageBuffer, Handle *thumbnailBuffer,
  465.                     CmPictureInfo *pictureInfo)
  466. {
  467.     OSErr                err;    
  468.     CmPictureInfo        tempPictureInfo;
  469.     Boolean                firstRead;
  470.     unsigned long        actualBytes;
  471.     
  472.     //    Initialize.
  473.     if ( imageBuffer != nil )
  474.         *imageBuffer = nil;
  475.     if ( thumbnailBuffer != nil )
  476.         *thumbnailBuffer = nil;
  477.  
  478.     //    Get picture info.
  479.     err = CmGetPictureInfo(camData, imageNumber, &tempPictureInfo);
  480.     if ( err != noErr )
  481.         goto bail;
  482.  
  483.     //    If pictureInfo is requested, return it.
  484.     if ( pictureInfo != nil )
  485.         *pictureInfo = tempPictureInfo;
  486.         
  487.     //    If imageBuffer is requested, return it.
  488.     if ( imageBuffer != nil ) {
  489.         *imageBuffer = NewHandle(tempPictureInfo.imageDataSize);
  490.         if ( *imageBuffer == nil ) {
  491.             err = memFullErr;
  492.             goto bail;
  493.         }
  494.         MoveHHi(*imageBuffer);
  495.         HLock(*imageBuffer);
  496.         firstRead = true;
  497.         err = CmGetFullSizeImage(camData, imageNumber, (unsigned char *) **imageBuffer, tempPictureInfo.imageDataSize, firstRead, &actualBytes);
  498.         HUnlock(*imageBuffer);
  499.         if ( err != noErr )
  500.             goto bail;
  501.         if ( actualBytes != tempPictureInfo.imageDataSize ) {
  502.             err = ioErr;
  503.             goto bail;
  504.         }
  505.     }
  506.     
  507.     //    If thumbnailBuffer is requested, return it.
  508.     if ( thumbnailBuffer != nil ) {
  509.         *thumbnailBuffer = NewHandle(2400);
  510.         if ( *imageBuffer == nil ) {
  511.             err = memFullErr;
  512.             goto bail;
  513.         }
  514.         MoveHHi(*thumbnailBuffer);
  515.         HLock(*thumbnailBuffer);
  516.         firstRead = true;
  517.         err = CmGetThumbnailImage(camData, imageNumber, (unsigned char *) **thumbnailBuffer, 2400, firstRead, &actualBytes);
  518.         HUnlock(*thumbnailBuffer);
  519.         if ( err != noErr )
  520.             goto bail;
  521.         if ( actualBytes != 2400 ) {
  522.             err = ioErr;
  523.             goto bail;
  524.         }
  525.     }
  526.     
  527. bail:
  528.     return ( err );
  529. }
  530.  
  531. /* ------------------------------------------------------------------------- */
  532.  
  533. /*
  534.     Description:    GetCameraInfo()
  535.                     Get camera info.
  536.  
  537.     Format Params:    
  538.         Name            Usage    Description/Assumptions
  539.         ----            ----    -----------------------
  540.         camData            PI        Valid camData from OpenCameraDriver.
  541.         cameraInfo        PO        Camera info returned.
  542.         
  543.     Usage: P=Parameter,R=ReturnValue,E=External,G=FileGlobal,L=Local,I=Input,O=Output
  544.     
  545.     Error Handling:    If noErr is returned, you can assume that everything requested has been returned.
  546.  
  547.     Special Notes:    xxx put other comments here xxx
  548.  
  549. */
  550.  
  551. OSErr GetCameraInfo(CameraData camData, CmCameraInfo *cameraInfo)
  552. {
  553.     OSErr                err;
  554.     
  555.     //    Check input parameters.
  556.     if ( cameraInfo == nil) {
  557.         err = paramErr;
  558.         goto bail;
  559.     }
  560.             
  561.     //    Get Camera Info.    
  562.     err = CmGetCameraInfo(camData, cameraInfo);
  563.     if ( err != noErr )
  564.         goto bail;
  565.  
  566. bail:
  567.     return ( err );
  568. }
  569.  
  570. /* ------------------------------------------------------------------------- */
  571.  
  572. /*
  573.     Description:    GetCameraMatrix()
  574.                     Get camera matrix.
  575.  
  576.     Format Params:    
  577.         Name            Usage    Description/Assumptions
  578.         ----            ----    -----------------------
  579.         camData            PI        Valid camData from OpenCameraDriver.
  580.         cameraMatrix    PO        Camera matrix returned.  If camera version is 1, then err = -1 is
  581.                                 returned.
  582.         
  583.     Usage: P=Parameter,R=ReturnValue,E=External,G=FileGlobal,L=Local,I=Input,O=Output
  584.     
  585.     Error Handling:    If noErr is returned, you can assume that everything requested has been returned.
  586.  
  587.     Special Notes:    xxx put other comments here xxx
  588.  
  589. */
  590.  
  591. OSErr GetCameraMatrix(CameraData camData, CmColorMatrixPtr colorMatrix)
  592. {
  593.     OSErr                err;
  594.     CmCameraInfo        cameraInfo;
  595.     
  596.     //    Check input parameters.
  597.     if ( colorMatrix == nil) {
  598.         err = paramErr;
  599.         goto bail;
  600.     }
  601.             
  602.     //    Get Camera Info.    
  603.     err = CmGetCameraInfo(camData, &cameraInfo);
  604.     if ( err != noErr )
  605.         goto bail;
  606.  
  607.     //    If version is not 1, then return color matrix, otherwise, return err = -1.
  608.     if ( cameraInfo.colorMatrixVersion[0] != 0 || cameraInfo.colorMatrixVersion[1] != 1 ) {
  609.         //    Get Camera Info.    
  610.         err = CmGetColorCorrectionMatrix(camData, colorMatrix);
  611.         if ( err != noErr )
  612.             goto bail;
  613.     } else
  614.         err = -1;
  615.  
  616. bail:        
  617.     return ( err );
  618. }
  619.  
  620. /* ------------------------------------------------------------------------- */
  621.  
  622. /*
  623.     Description:    GetCameraDecompTable()
  624.                     Get camera decompression table.
  625.  
  626.     Format Params:    
  627.         Name            Usage    Description/Assumptions
  628.         ----            ----    -----------------------
  629.         camData            PI        Valid camData from OpenCameraDriver.
  630.         decompTable    PO            Camera decomp table returned.  If camera version is 1, then
  631.                                 err = -1 is returned.
  632.         
  633.     Usage: P=Parameter,R=ReturnValue,E=External,G=FileGlobal,L=Local,I=Input,O=Output
  634.     
  635.     Error Handling:    If noErr is returned, you can assume that everything requested has been returned.
  636.  
  637.     Special Notes:    xxx put other comments here xxx
  638.  
  639. */
  640.  
  641. OSErr GetCameraDecompTable(CameraData camData, BufferPtr decompTable)
  642. {
  643.     OSErr                err;
  644.     CmCameraInfo        cameraInfo;
  645.     
  646.     //    Check input parameters.
  647.     if ( decompTable == nil) {
  648.         err = paramErr;
  649.         goto bail;
  650.     }
  651.             
  652.     //    Get Camera Info.    
  653.     err = CmGetCameraInfo(camData, &cameraInfo);
  654.     if ( err != noErr )
  655.         goto bail;
  656.  
  657.     //    If version is not 1, then return color matrix, otherwise, return err = -1.
  658.     if ( cameraInfo.compDecompParamVersion[0] != 0 || cameraInfo.compDecompParamVersion[1] != 1 ) {
  659.         //    Get Camera Info.    
  660.         err = CmGetDecompTable(camData, decompTable);
  661.         if ( err != noErr )
  662.             goto bail;
  663.     } else
  664.         err = -1;
  665.  
  666. bail:
  667.     return ( err );
  668. }
  669.  
  670. /* ------------------------------------------------------------------------- */
  671. /* ------------------------------------------------------------------------- */
  672. /* ------------------------------------------------------------------------- */
  673.  
  674. //    These routines are complete.  You don't have connect or disconnect to the camera.  These
  675. //    routines are useful if you call it only once.  However, if you plan to call it multiple times,
  676. //    then it is more efficient to connect and disconnect yourself only once.
  677.  
  678. /*
  679.     Description:    GrabOnePictureComplete()
  680.                     Given and image number, load a pict, thumbnail,
  681.                     thumbnailHeader, and pictureInfo as requested.
  682.  
  683.     Format Params:    
  684.         Name            Usage    Description/Assumptions
  685.         ----            ----    -----------------------
  686.         imageNumber        PI        Pass in an image number.
  687.         pict            PO        If pict is not nil, then it will be returned.  The pict is unlocked.
  688.         thumbnail        PO        thumbnail is not nil, then it will be returned.  The thumbnail is unlocked.
  689.         thumbnailHeader    PO        If thumbnailHeader is not nil then it will be returned.
  690.         pictureInfo        PO        if pictureInfo is not nil, then it will be returned.
  691.                                 
  692.     Usage: P=Parameter,R=ReturnValue,E=External,G=FileGlobal,L=Local,I=Input,O=Output
  693.     
  694.     Error Handling:    If noErr is returned, you can assume that everything requested has been returned.
  695.  
  696.     Special Notes:    xxx put other comments here xxx
  697.  
  698. */
  699.  
  700. OSErr GrabOnePictureComplete(short imageNumber, PicHandle *pict, Handle *thumbnail,
  701.                         ThumbnailHeader *thumbnailHeader, CmPictureInfo *pictureInfo)
  702. {
  703.     OSErr                    err;
  704.     CameraData                camData;
  705.     CmPortInfoHandle        portInfoHandle;
  706.     
  707.     Handle                    imageBuffer;
  708.     Handle                    *imageBufferPtr;
  709.     Handle                    thumbnailBuffer;
  710.     Handle                    *thumbnailBufferPtr;
  711.     short                    cameraType;
  712.     ImageHeader                imageHeader;
  713.     CmPictureInfo            tempPictureInfo;
  714.     CmColorMatrix            colorMatrix;
  715.     BufferPtr                decompTable;
  716.     Boolean                    customColorMatrix;
  717.     
  718.     //    Open camera driver.  If can't open camera, then return immediately.
  719.     err = OpenCameraDriver(&camData);
  720.     if ( err != noErr )
  721.         return ( err );
  722.         
  723.     //    Make some temporary variables.
  724.     imageBuffer = nil;
  725.     if ( pict != nil ) {
  726.         imageBufferPtr = &imageBuffer;
  727.     } else
  728.         imageBufferPtr = nil;
  729.     thumbnailBuffer = nil;
  730.     if ( thumbnail != nil ) {
  731.         thumbnailBufferPtr = &thumbnailBuffer;
  732.     } else
  733.         thumbnailBufferPtr = nil;
  734.         
  735.     //    Connect to camera.
  736.     err = ConnectCamera(camData, &portInfoHandle, &cameraType);
  737.     if ( err != noErr )
  738.         goto bail;
  739.     
  740.     //    Get image or thumbnail as requested.
  741.     err = GrabOneImage(camData, imageNumber, imageBufferPtr, thumbnailBufferPtr, &tempPictureInfo);
  742.     if ( err != noErr )
  743.         goto bail;
  744.         
  745.     //    Make sure data is returned properly.
  746.     if ( pict != nil ) {
  747.         if (imageBuffer == nil ) {
  748.             err = memFullErr;
  749.             goto bail;
  750.         }
  751.     }
  752.     if ( thumbnail != nil ) {
  753.         if (thumbnailBuffer == nil ) {
  754.             err = memFullErr;
  755.             goto bail;
  756.         }
  757.     }
  758.     
  759.     //    Build imageHeader no matter what.
  760.     if ( cameraType == kVenusCamera ) {
  761.         imageHeader.signature = 'qktk';
  762.         imageHeader.version = 1;
  763.     } else if  ( cameraType == kNimbusCamera ) {
  764.         imageHeader.signature = 'qktn';
  765.         imageHeader.version = 2;
  766.     } else {
  767.         err = paramErr;
  768.         goto bail;
  769.     }
  770.     if ( tempPictureInfo.pictureMode == 16 )
  771.         imageHeader.flags = kQktkHiResImage;
  772.     else if ( tempPictureInfo.pictureMode == 32 )
  773.         imageHeader.flags = kQktkStdResImage;
  774.     else {
  775.         err = paramErr;
  776.         goto bail;
  777.     }
  778.     imageHeader.dataSize = tempPictureInfo.imageDataSize;
  779.         
  780.     customColorMatrix = false;
  781.     if ( pict != nil ) {
  782.         //    Get matrix if it is newer and if a pict is requested.
  783.         err = GetCameraMatrix(camData, &colorMatrix);
  784.         if ( err == noErr )
  785.             customColorMatrix = true;
  786.         else if ( err != -1 )
  787.             goto bail;
  788.  
  789.         //    Get decompTable if it is newer and if a pict is requested.
  790.         if ( cameraType == kVenusCamera ) {
  791.             decompTable = (BufferPtr) NewPtr(kCmDecompTableSize100);
  792.         } else if  ( cameraType == kNimbusCamera ) {
  793.             decompTable = (BufferPtr) NewPtr(kCmDecompTableSize150);
  794.         } else {
  795.             err = paramErr;
  796.             goto bail;
  797.         }
  798.         if ( decompTable == nil ) {
  799.             err = memFullErr;
  800.             goto bail;
  801.         }
  802.         err = GetCameraDecompTable(camData, decompTable);
  803.         if ( err == -1 ) {
  804.             if ( decompTable != nil )
  805.                 DisposePtr((Ptr) decompTable);
  806.             decompTable = nil;
  807.         } else if ( err != noErr )
  808.             goto bail;
  809.     }
  810.     
  811.     //    Make the pict using imageHeader.
  812.     if ( pict != nil ) {
  813.         CmColorMatrixPtr            colorMatrixPtr;
  814.         
  815.         if ( customColorMatrix )
  816.             colorMatrixPtr = &colorMatrix;
  817.         else
  818.             colorMatrixPtr = nil;
  819.         HLock(imageBuffer);
  820.         //    MakeQuickTakePicture returns an unlocked pict.
  821.         err = MakeQuickTakePicture(&imageHeader, *imageBuffer, colorMatrixPtr, decompTable, pict);
  822.         HUnlock(imageBuffer);
  823.         if ( err != noErr )
  824.             goto bail;
  825.     }
  826.     
  827.     //    Make thumbnail.
  828.     if ( thumbnail != nil ) {
  829.         HLock(thumbnailBuffer);
  830.         *thumbnail = thumbnailBuffer;
  831.         HUnlock(thumbnailBuffer);
  832.     }
  833.  
  834.     //    Make thumbnail header.
  835.     if ( thumbnailHeader != nil ) {
  836.         thumbnailHeader->signature = imageHeader.signature;
  837.         thumbnailHeader->version = imageHeader.version;
  838.         thumbnailHeader->flags = kQktkThumbnailImage | (imageHeader.flags & kQktkRotationBits);
  839.         thumbnailHeader->dataSize = 2400;
  840.     }
  841.     
  842.     //    Fill in pictureInfo.
  843.     if ( pictureInfo != nil ) {
  844.         *pictureInfo = tempPictureInfo;
  845.     }
  846.     
  847.     err = noErr;
  848.     
  849. bail:
  850.     //    imageBuffer is no longer needed because it is stored into Picture.
  851.     if ( imageBuffer != nil )
  852.         DisposHandle(imageBuffer);
  853.         
  854.     //    If we failed at any point, remember to close camera.  Otherwise, it won't open up the next time.
  855.     DisconnectCamera(camData, portInfoHandle);
  856.     CloseCameraDriver(camData);
  857.  
  858.     return ( err );
  859. }
  860.  
  861. /* ------------------------------------------------------------------------- */
  862.  
  863. /*
  864.     Description:    GrabOneImageComplete()
  865.                     Given and image number, load imageBuffer, thumbnailBuffer, pictureInfo, and
  866.                     cameraType as requested.
  867.  
  868.     Format Params:    
  869.         Name            Usage    Description/Assumptions
  870.         ----            ----    -----------------------
  871.         imageNumber        PI        Pass in an image number.
  872.         imageBuffer        PO        If imageBuffer is not nil, then it will be returned.
  873.         thumbnailBuffer    PO        If thumbnailBuffer is not nil, then it will be returned.
  874.         pictureInfo        PO        pictureInfo is not nil, then then it will be returned.
  875.         cameraType        PO        If cameraType is not nil then it will be returned.
  876.                                 
  877.     Usage: P=Parameter,R=ReturnValue,E=External,G=FileGlobal,L=Local,I=Input,O=Output
  878.     
  879.     Error Handling:    If noErr is returned, you can assume that everything requested has been returned.
  880.  
  881.     Special Notes:    xxx put other comments here xxx
  882.  
  883. */
  884.  
  885. OSErr GrabOneImageComplete(short imageNumber, Handle *imageBuffer, Handle *thumbnailBuffer,
  886.                     CmPictureInfo *pictureInfo, short *cameraType)
  887. {
  888.     OSErr                err;
  889.     CameraData            camData;
  890.     CmPortInfoHandle    portInfoHandle;
  891.     
  892.     CmPictureInfo        tempPictureInfo;
  893.     Boolean                firstRead;
  894.     unsigned long        actualBytes;
  895.     
  896.     //    Open camera driver.  If can't open camera, then return immediately.
  897.     err = OpenCameraDriver(&camData);
  898.     if ( err != noErr )
  899.         return ( err );
  900.         
  901.     //    Initialize.
  902.     if ( imageBuffer != nil )
  903.         *imageBuffer = nil;
  904.     if ( thumbnailBuffer != nil )
  905.         *thumbnailBuffer = nil;
  906.     if ( cameraType != nil )
  907.         *cameraType = kUnknownCamera;
  908.  
  909.     //    Connect to camera.
  910.     err = ConnectCamera(camData, &portInfoHandle, cameraType);
  911.     if ( err != noErr )
  912.         goto bail;    
  913.     
  914.     //    Get picture info.
  915.     err = CmGetPictureInfo(camData, imageNumber, &tempPictureInfo);
  916.     if ( err != noErr )
  917.         goto bail;
  918.  
  919.     //    If pictureInfo is requested, return it.
  920.     if ( pictureInfo != nil )
  921.         *pictureInfo = tempPictureInfo;
  922.         
  923.     //    If imageBuffer is requested, return it.
  924.     if ( imageBuffer != nil ) {
  925.         *imageBuffer = NewHandle(tempPictureInfo.imageDataSize);
  926.         if ( *imageBuffer == nil ) {
  927.             err = memFullErr;
  928.             goto bail;
  929.         }
  930.         MoveHHi(*imageBuffer);
  931.         HLock(*imageBuffer);
  932.         firstRead = true;
  933.         err = CmGetFullSizeImage(camData, imageNumber, (unsigned char *) **imageBuffer, tempPictureInfo.imageDataSize, firstRead, &actualBytes);
  934.         HUnlock(*imageBuffer);
  935.         if ( err != noErr )
  936.             goto bail;
  937.         if ( actualBytes != tempPictureInfo.imageDataSize ) {
  938.             err = ioErr;
  939.             goto bail;
  940.         }
  941.     }
  942.     
  943.     //    If thumbnailBuffer is requested, return it.
  944.     if ( thumbnailBuffer != nil ) {
  945.         *thumbnailBuffer = NewHandle(2400);
  946.         if ( *imageBuffer == nil ) {
  947.             err = memFullErr;
  948.             goto bail;
  949.         }
  950.         MoveHHi(*thumbnailBuffer);
  951.         HLock(*thumbnailBuffer);
  952.         firstRead = true;
  953.         err = CmGetThumbnailImage(camData, imageNumber, (unsigned char *) **thumbnailBuffer, 2400, firstRead, &actualBytes);
  954.         HUnlock(*thumbnailBuffer);
  955.         if ( err != noErr )
  956.             goto bail;
  957.         if ( actualBytes != 2400 ) {
  958.             err = ioErr;
  959.             goto bail;
  960.         }
  961.     }
  962.     
  963. bail:
  964.     //    If we failed at any point, remember to close camera.  Otherwise, it won't open up the next time.
  965.     DisconnectCamera(camData, portInfoHandle);
  966.     CloseCameraDriver(camData);
  967.     return ( err );
  968. }
  969.  
  970. /* ------------------------------------------------------------------------- */
  971.  
  972. /*
  973.     Description:    GetCameraInfoComplete()
  974.                     Get camera info.
  975.  
  976.     Format Params:    
  977.         Name            Usage    Description/Assumptions
  978.         ----            ----    -----------------------
  979.         cameraInfo        PO        Camera info returned.
  980.                                 
  981.     Usage: P=Parameter,R=ReturnValue,E=External,G=FileGlobal,L=Local,I=Input,O=Output
  982.     
  983.     Error Handling:    If noErr is returned, you can assume that everything requested has been returned.
  984.  
  985.     Special Notes:    xxx put other comments here xxx
  986.  
  987. */
  988.  
  989. OSErr GetCameraInfoComplete(CmCameraInfo *cameraInfo)
  990. {
  991.     OSErr                err;
  992.     CameraData            camData;
  993.     CmPortInfoHandle    portInfoHandle;
  994.     
  995.     //    Check input parameters.
  996.     if ( cameraInfo == nil) {
  997.         err = paramErr;
  998.         goto bail;
  999.     }
  1000.     
  1001.     //    Open camera driver.  If can't open camera, then return immediately.
  1002.     err = OpenCameraDriver(&camData);
  1003.     if ( err != noErr )
  1004.         return ( err );
  1005.         
  1006.     //    Connect to camera.
  1007.     err = ConnectCamera(camData, &portInfoHandle, nil);
  1008.     if ( err != noErr )
  1009.         goto bail;    
  1010.     
  1011.     //    Get Camera Info.    
  1012.     err = CmGetCameraInfo(camData, cameraInfo);
  1013.     if ( err != noErr )
  1014.         goto bail;
  1015.  
  1016. bail:
  1017.     //    If we failed at any point, remember to close camera.  Otherwise, it won't open up the next time.
  1018.     DisconnectCamera(camData, portInfoHandle);
  1019.     CloseCameraDriver(camData);
  1020.     
  1021.     return ( err );    
  1022. }
  1023.  
  1024. /* ------------------------------------------------------------------------- */
  1025.  
  1026. /*
  1027.     Description:    GetCameraMatrixComplete()
  1028.                     Get camera matrix.
  1029.  
  1030.     Format Params:    
  1031.         Name            Usage    Description/Assumptions
  1032.         ----            ----    -----------------------
  1033.         cameraMatrix    PO        Camera matrix returned.  If camera version is 1, then err = -1 is
  1034.                                 returned.
  1035.         
  1036.     Usage: P=Parameter,R=ReturnValue,E=External,G=FileGlobal,L=Local,I=Input,O=Output
  1037.     
  1038.     Error Handling:    If noErr is returned, you can assume that everything requested has been returned.
  1039.  
  1040.     Special Notes:    xxx put other comments here xxx
  1041.  
  1042. */
  1043.  
  1044. OSErr GetCameraMatrixComplete(CmColorMatrixPtr colorMatrix)
  1045. {
  1046.     OSErr                err;
  1047.     CameraData            camData;
  1048.     CmPortInfoHandle    portInfoHandle;
  1049.     CmCameraInfo        cameraInfo;
  1050.  
  1051.     //    Check input parameters.
  1052.     if ( colorMatrix == nil) {
  1053.         err = paramErr;
  1054.         goto bail;
  1055.     }
  1056.     
  1057.     //    Open camera driver.  If can't open camera, then return immediately.
  1058.     err = OpenCameraDriver(&camData);
  1059.     if ( err != noErr )
  1060.         return ( err );
  1061.         
  1062.     //    Connect to camera.
  1063.     err = ConnectCamera(camData, &portInfoHandle, nil);
  1064.     if ( err != noErr )
  1065.         goto bail;    
  1066.     
  1067.     //    Get Camera Info.    
  1068.     err = CmGetCameraInfo(camData, &cameraInfo);
  1069.     if ( err != noErr )
  1070.         goto bail;
  1071.  
  1072.     //    If version is not 1, then return color matrix, otherwise, return err = -1.
  1073.     if ( cameraInfo.colorMatrixVersion[0] != 0 || cameraInfo.colorMatrixVersion[1] != 1 ) {
  1074.         //    Get Camera Info.    
  1075.         err = CmGetColorCorrectionMatrix(camData, colorMatrix);
  1076.         if ( err != noErr )
  1077.             goto bail;
  1078.     } else
  1079.         err = -1;
  1080.  
  1081. bail:
  1082.     //    If we failed at any point, remember to close camera.  Otherwise, it won't open up the next time.
  1083.     DisconnectCamera(camData, portInfoHandle);
  1084.     CloseCameraDriver(camData);
  1085.     
  1086.     return ( err );    
  1087. }
  1088.  
  1089. /* ------------------------------------------------------------------------- */
  1090.  
  1091. /*
  1092.     Description:    GetCameraDecompTableComplete()
  1093.                     Get camera decompression table.
  1094.  
  1095.     Format Params:    
  1096.         Name            Usage    Description/Assumptions
  1097.         ----            ----    -----------------------
  1098.         decompTable    PO            Camera decomp table returned.  If camera version is 1, then
  1099.                                 err = -1 is returned.
  1100.         
  1101.     Usage: P=Parameter,R=ReturnValue,E=External,G=FileGlobal,L=Local,I=Input,O=Output
  1102.     
  1103.     Error Handling:    If noErr is returned, you can assume that everything requested has been returned.
  1104.  
  1105.     Special Notes:    xxx put other comments here xxx
  1106.  
  1107. */
  1108.  
  1109. OSErr GetCameraDecompTableComplete(BufferPtr decompTable)
  1110. {
  1111.     OSErr                err;
  1112.     CameraData            camData;
  1113.     CmPortInfoHandle    portInfoHandle;
  1114.     CmCameraInfo        cameraInfo;
  1115.  
  1116.     //    Check input parameters.
  1117.     if ( decompTable == nil) {
  1118.         err = paramErr;
  1119.         goto bail;
  1120.     }
  1121.     
  1122.     //    Open camera driver.  If can't open camera, then return immediately.
  1123.     err = OpenCameraDriver(&camData);
  1124.     if ( err != noErr )
  1125.         return ( err );
  1126.         
  1127.     //    Connect to camera.
  1128.     err = ConnectCamera(camData, &portInfoHandle, nil);
  1129.     if ( err != noErr )
  1130.         goto bail;    
  1131.     
  1132.     //    Get Camera Info.    
  1133.     err = CmGetCameraInfo(camData, &cameraInfo);
  1134.     if ( err != noErr )
  1135.         goto bail;
  1136.  
  1137.     //    If version is not 1, then return color matrix, otherwise, return err = -1.
  1138.     if ( cameraInfo.compDecompParamVersion[0] != 0 || cameraInfo.compDecompParamVersion[1] != 1 ) {
  1139.         //    Get Camera Info.    
  1140.         err = CmGetDecompTable(camData, decompTable);
  1141.         if ( err != noErr )
  1142.             goto bail;
  1143.     } else
  1144.         err = -1;
  1145.  
  1146. bail:
  1147.     //    If we failed at any point, remember to close camera.  Otherwise, it won't open up the next time.
  1148.     DisconnectCamera(camData, portInfoHandle);
  1149.     CloseCameraDriver(camData);
  1150.     
  1151.     return ( err );    
  1152. }
  1153.  
  1154. /* ------------------------------------------------------------------------- */
  1155. /* ------------------------------------------------------------------------- */
  1156. /* ------------------------------------------------------------------------- */
  1157.  
  1158. /*
  1159.     Description:    GetPictureFrame()
  1160.                     Get correct picture frame size.  This is necessary because
  1161.                     some pictures may be 320x240 with 144 dpi.  This routine will
  1162.                     give you the recommended picture frame, or the 'best' picture frame.
  1163.  
  1164.     Format Params:    
  1165.         Name            Usage    Description/Assumptions
  1166.         ----            ----    -----------------------
  1167.         pict            PI        Must be valid picture.
  1168.         exeryPixel        PI        If true, then return 'best'.  Otherwise, use normal picFrame.
  1169.         myRect            PO        Returned rect.
  1170.         
  1171.     Usage: P=Parameter,R=ReturnValue,E=External,G=FileGlobal,L=Local,I=Input,O=Output
  1172.     
  1173.     Error Handling:    If noErr is returned, you can assume that everything requested has been returned.
  1174.  
  1175.     Special Notes:    xxx put other comments here xxx
  1176.  
  1177. */
  1178.  
  1179. typedef        struct    {
  1180.     short    picSize; 
  1181.     Rect    picFrame;
  1182.     short    picVersionOp;
  1183.     short    picVersion;
  1184.     short    picHeaderOp;
  1185.     short    picHeaderVersion;
  1186.     short    picReserved1;
  1187.     Fixed    picHRes;
  1188.     Fixed    picVRes;
  1189.     Rect     picSrcRect;         
  1190.     long    picReserved2;
  1191. } ColorPicture;
  1192.  
  1193. void GetPictureFrame(PicHandle pict, Boolean everyPixel, Rect *myRect)
  1194. {
  1195.     if ( pict != nil && myRect != nil ) {
  1196.         if ( everyPixel ) {
  1197.             ColorPicture    *newPict;
  1198.             
  1199.             newPict = (ColorPicture *) *pict;
  1200.             if ( newPict->picVersion > 0x200 && newPict->picHeaderOp == 0x0c00 &&
  1201.                         newPict->picHeaderVersion == -2 )
  1202.                 *myRect = newPict->picSrcRect;
  1203.             else
  1204.                 *myRect = newPict->picFrame;
  1205.         } else {
  1206.             *myRect = (**pict).picFrame;
  1207.         }
  1208.     }
  1209. }
  1210.